﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GUI
{
    public partial class FormMuaVe : Form
    {
        public FormMuaVe()
        {
            InitializeComponent();
        }

        private void btnChonMon_Click(object sender, EventArgs e)
        {
            FormTraCuuMonAn f = new FormTraCuuMonAn();
            f.ShowDialog();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        long TongTien = 0;
        private void FormMuaVe_Load(object sender, EventArgs e)
        {
            List<MonAnDTO> listMONAN = new List<MonAnDTO>();
            listMONAN = FormTraCuuMonAn.listChonMonAn;
            int STT = 0;
            foreach(MonAnDTO singleRecord in listMONAN)
            {
                TongTien = TongTien + singleRecord.Don_Gia * singleRecord.So_Luong;
                string MoneyFormat = string.Format("{0:#,##0 VND;(#,##0 VND);''}", singleRecord.Don_Gia * singleRecord.So_Luong);
                dataGridView1.Rows.Add(STT + 1, singleRecord.Id_Mon_An, singleRecord.Ten_Mon_An, singleRecord.So_Luong, string.Format("{0:#,##0 VND;(#,##0 VND);''}", singleRecord.Don_Gia), MoneyFormat);
                STT++;
            }
            txtTongTien.Text = string.Format("{0:#,##0 VND;(#,##0 VND);''}",TongTien);
        }

        private void btnTinhTien_Click(object sender, EventArgs e)
        {
            
            
            // Ghi Xuống bảng HD
            DateTime dtGioRa = DateTime.Now;
            HoaDonDTO hdDTO = new HoaDonDTO();
            hdDTO.Gio_Ra = dtGioRa;
            hdDTO.Ngay_Lap = dtGioRa.Date;
            HoaDonBUS hdBUS = new HoaDonBUS();
            hdBUS.GhiXuongBangHoaDonMuaVe(hdDTO);
            // Ghi Xuống Bảng CT Hóa Đơn
            HoaDonBUS hdBUS2 = new HoaDonBUS();
            int ID_Hoa_Don_Moi_Nhat = hdBUS2.IdHoaDonMoiNhat();
            
            int STT = 0;
            foreach ( MonAnDTO row in FormTraCuuMonAn.listChonMonAn)
            {
                ChiTietHoaDonDTO singleRecordCT = new ChiTietHoaDonDTO();
                singleRecordCT.Id_Mon_An = row.Id_Mon_An;
                singleRecordCT.Id_Hoa_Don = ID_Hoa_Don_Moi_Nhat;
                singleRecordCT.So_Luong = row.So_Luong;
                singleRecordCT.Don_Gia = row.Don_Gia;
                singleRecordCT.Thanh_Tien = singleRecordCT.Don_Gia * singleRecordCT.So_Luong;
                ChiTietHoaDonBUS ctHDBUS = new ChiTietHoaDonBUS();
                ctHDBUS.InsertBangChiTiethoaDon(singleRecordCT);
                STT++;
            }
            // Ghi Xuống Bảng Tính Tiền với Phương Thức Mua = 1
            TinhTienDTO ttDTO = new TinhTienDTO();
            ttDTO.Khuyen_Mai = txtKhuyenMai.Text;
            ttDTO.Tong_So_Tien = TongTien;
            ttDTO.Thanh_Tien = tempTongTien;
            ttDTO.Id_Hoa_Don = ID_Hoa_Don_Moi_Nhat;

            TinhTienBUS ttBUS1 = new TinhTienBUS();
            ttBUS1.GhiXuongBangTinhTien(ttDTO,1);

            MessageBox.Show("Tính Tiền Thành Công");
            this.Close();
        }
        long tempTongTien;
        private void cbTinhTien_CheckedChanged(object sender, EventArgs e)
        {
            if(cbTinhTien.Checked)
            {
                txtVAT.Enabled = false;
                txtKhuyenMai.Enabled = false;
                btnTinhTien.Enabled = true;
                tempTongTien = TongTien;
                int VAT;
                int KHUYENMAI;
                try
                {
                    if (txtVAT.Text == "")
                    {
                        VAT = 0;
                    }
                    else
                    {
                        VAT = Convert.ToInt32(txtVAT.Text);
                    }
                    if (txtKhuyenMai.Text == "")
                    {
                        KHUYENMAI = 0;
                    }
                    else
                    {
                        KHUYENMAI = Convert.ToInt32(txtKhuyenMai.Text);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("ô Khuyến Mãi hoặc VAT có chứa ký tự, vui lòng nhập lại!");
                    return;
                }
                long nKHUYENMAI = Convert.ToInt64((float)(KHUYENMAI * tempTongTien) / 100);
                tempTongTien = tempTongTien - nKHUYENMAI;
                long nVAT = Convert.ToInt64((float)(VAT * tempTongTien) / 100);
                tempTongTien = tempTongTien + nVAT;
                string MoneyFormat = string.Format("{0:#,##0 VND;(#,##0 VND);''}", tempTongTien);
                txtThanhTien.Text = MoneyFormat;
            }
            else if(!(cbTinhTien.Checked))
            {
                txtVAT.Enabled = true;
                txtKhuyenMai.Enabled = true;
                btnTinhTien.Enabled = false;
            }
        }
    }
}
